package com.erpoa.mapper;

import com.erpoa.entity.FMachineCode;
import org.apache.ibatis.annotations.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author chenjimin
 * @Title: PackServletMapper
 * @ProjectName oaerp
 * @Description: TODO
 * @date 2021/2/1810:25
 */
@Mapper
public interface PackServletMapper {

    FMachineCode checkMachineAndBarCodevaild(String barcode);


    void callChkCanConfirm(@Param("map") HashMap map);

    @Insert("insert into outControlRecords("
            + "fid,version,fmachinecode,forderno,fbarcode,FOPERATORCODE,fdifference,fendbarcode,"
            + "FCREATEPER,FCREATEDATE,ffactorycode,fworkshopcode) " + "values(#{uuid},0,#{machine},#{mLOrderNo},#{barcode},#{jobnum},"
            + "#{qnflag},#{sCode},#{oweMatWeight},sysdate,#{mLFactoryCode},#{mLWorkShopCode})")
    int finsertoutcrs(@Param("uuid") String uuid, @Param("machine") String machine, @Param("mLOrderNo") String mLOrderNo, @Param("barcode") String barcode,
                      @Param("jobnum") String jobnum, @Param("qnflag") String qnflag, @Param("sCode") String sCode, @Param("oweMatWeight") float oweMatWeight,
                      @Param("mLFactoryCode") String mLFactoryCode, String mLWorkShopCode);

    @Select("select tc_memb06 from ds2.tc_memb_file where tc_memb01='Q' and tc_memb02='E' and tc_memb03=#{machine} and tc_memb04=#{jobnum} and tc_memb07='Y'")
    List<String> selectmembByMachJobnum(@Param("machine") String machine, @Param("jobnum") String jobnum);

    @Select("select nvl(fqty,0) from MG_ProcessBarCode where fBarCode = #{barcode}  and fhangup is null ")
    Integer selectFqtyBybarCode(String barcode);

    @Insert("insert into MG_ProcessBarCode " + " (fid,version,fplanid,fbarcode,fmachinecode, "
            + " forderno,fmatcode,fmatname,ftype,fmodelno,fwarecode,fseatcode, "
            + " fproportion,fqty,fweight,fitem,fhangup, "
            + " fcreateper,fcreatedate,ffactorycode,ffactoryname,fworkshopcode,fworkshopname, "
            + " fprocesscode,fprocessname,fbarcodetype,fplanqty,fOperatorCode,fLables,fEndProc,fcolor) "
            + " select #{uuid},0,fplanid,fbarcode,fmachinecode, "
            + " forderno,fmatcode,fmatname,ftype,fmodelno,fwarecode,fseatcode, "
            + " fproportion,#{avg} as fqty,round(#{avg}/fproportion,3) as fweight,(select count(*) "
            + "from MG_ProcessBarCode where fbarcode=#{barcode})+1 as fitem,#{machine} as fhangup, "
            + " #{sCode} as fcreateper,sysdate as fcreatedate,ffactorycode,ffactoryname,fworkshopcode,fworkshopname, "
            + " fprocesscode,fprocessname,fbarcodetype,fplanqty,#{jobnums} as fOperatorCode,1 as fLables,fEndProc,fcolor "
            + " from MG_ProcessBarCode where fbarcode = #{barcode} and fhangup is null and fBarCodeType = 0 ")
    int insertProcessByParam(@Param("uuid") String uuid, @Param("avg") int avg, @Param("barcode") String barcode, @Param("machine") String machine,
                             @Param("sCode") String sCode, @Param("jobnums") String jobnums);

    @Update("update MG_ProcessBarCode set fOperatorCode = #{jobnums} , fqty=#{last}"
            + " where fBarCode = #{barcode} and fMachineCode=#{machine} and "
            + "fhangup is null and fconfirm is null and fOperatorCode is null")
    int updateProcessByParam(@Param("jobnums") String jobnums, @Param("barcode") String barcode, @Param("machine") String machine, @Param("last") int last);

    @Update("update MG_ProcessBarCode set fOperatorCode = #{jobnum} "
            + " where fBarCode = #{barcode} and fMachineCode=#{machine} and "
            + "fhangup is null and fconfirm is null and fbarcodetype = 0")
    int updateProcessBarByparamfirst(@Param("jobnum") String jobnum, @Param("barcode") String barcode, @Param("machine") String machine);

    @Update("update mg_orderplan set fStartDate = sysdate where fStartDate is null and "
            + "forderstatus = '生产中'  and fID = #{mLPlanID}")
    int updateorderplanByPlanId(String mLPlanID);


    void callChkEndCanConfirm(@Param("map") HashMap map);

    @Insert("insert into outControlRecords("
            + "fid,version,fmachinecode,forderno,fbarcode,FOPERATORCODE,fdifference,fendbarcode,"
            + "FCREATEPER,FCREATEDATE,ffactorycode,fworkshopcode) " + "values(#{uuid},0,#{machine},#{mLOrderNo},#{barcode},#{jobnum},"
            + "#{oweBackMatWeightTemp},#{qnTemp},#{sCode},sysdate,#{mLFactoryCode},#{mLWorkShopCode})")
    int insertOutControlBuParam(@Param("uuid") String uuid, @Param("machine") String machine, @Param("mLOrderNo") String mLOrderNo,
                                @Param("barcode") String barcode, @Param("jobnum") String jobnum,
                                @Param("qnTemp") String qnTemp, @Param("sCode") String sCode,
                                @Param("mLFactoryCode") String mLFactoryCode, @Param("mLWorkShopCode") String mLWorkShopCode,
                                @Param("oweBackMatWeightTemp") Float oweBackMatWeightTemp);

    @Select("Select nvl(sum(fqty),0) from dsx5.mg_processbarcode"
            + " where fplanid=(Select distinct fplanid from dsx5.mg_processbarcode where fbarcode =#{barcode}" +
            "  ) and fconfirm =1 and (fdefective is null or fdefective <> 1)")
    int selectsumfqtysecond(String barcode);

    @Update("update mg_orderplan " + " set fFinshQty = #{fqty} + #{mBarTotalNumtemp} "
            + " ,funfinQty = nvl(fplanqty,0) - #{fqty} - #{lqty}, "
            + " forderstatus = case when nvl(fplanqty,0) - #{fqty} - #{lqty} <= 0 then '已完成' else '生产中' end, "
            + " ffinishdate = case when nvl(fplanqty,0) - #{fqty} - #{lqty} <= 0  then sysdate else null end "
            + " where fid = #{mLPlanID} ")
    int updateorplanByParamsecond(@Param("fqty") int fqty, @Param("lqty") int lqty, @Param("mLPlanID") String mLPlanID);

    @Update("update MG_ProcessBarCode set fConfirm = 1,fConfirmScaner = #{sCode}, fFinishDate = sysdate "
            + " where fBarCode = #{barcode} and fMachineCode=#{machine} ")
    int updateprocessByParamsecond(@Param("sCode") String sCode, @Param("barcode") String barcode, @Param("machine") String machine);


    void callChkCCanConfirm(@Param("map") HashMap map);

    @Select("select fplanqty from dsx5.mg_orderplan where fid = #{mLPlanID} and FORDERSTATUS ='生产中'")
    Integer selectfplanqty(String mLPlanID);

    @Select(" select sum(fqty) from dsx5.mg_processbarcode where fplanid=#{mLPlanID} and fconfirm=1 and (FDEFECTIVE is null or FDEFECTIVE<>1) ")
    int selectProBarByPlanId(String mLPlanID);

    @Select("select sum(fqty) FQTY,FORDERNO from dsx5.mg_processbarcode where FBARCODE = #{barcode} and (FDEFECTIVE is null or FDEFECTIVE<>1) group by FORDERNO ")
    HashMap selectqtynoByBarCode(String barcode);

    @Select("select SFA03,SFA161 from ds2.sfa_file  where SFA12 = 'KG' and sfa01 = #{fOrderNo}")
    HashMap selectSfaSqlByfOrderNo(String fOrderNo);

    @Select("select NVL(SUM(CASE FOPTYPE WHEN '领料' THEN FRSPQTY WHEN '退料' THEN FRSPQTY*-1 END),0) " +
            " from dsx5.mg_recmat  A where  A.FMATCODE = #{fMatCode} AND FPLANID =#{mLPlanID} ")
    Float sumfrspqtyByMplanid(@Param("fMatCode") String fMatCode, @Param("mLPlanID") String mLPlanID);


    void callChkCProCanConfirm(@Param("map") HashMap map);


    void callChkECanConfirm(@Param("map") HashMap map);


    void callChk3CanConfirm(@Param("map") HashMap map);
}
